package mysql

import "common/models"

func (d Mysql) CreateUser(user *models.UserInfo) (err error) {
	return d.Db.Table("user_info").Create(&user).Error
}
func (d Mysql) QueryUserALlPage(page, size int) (res []*models.UserInfo, total int64, err error) {
	err = d.Db.Table("user_info").Offset((page - 1) * size).Limit(size).Find(&res).Count(&total).Error
	return
}
func (d Mysql) CheckUsernameExist(username string) (exist bool, err error) {
	var count int64
	exist = false
	err = d.Db.Table("user_info").Where("user_name = ?", username).Count(&count).Error
	if err != nil {
		return
	}
	if count == 0 {
		return
	}
	exist = true
	return
}
func (d Mysql) QueryUserInfoByUsername(username string) (res *models.UserInfo, err error) {
	err = d.Db.Table("user_info").Where("user_name = ?", username).First(&res).Error
	return
}
func (d Mysql) QueryUserInfoByID(id string) (res *models.UserInfo, err error) {
	err = d.Db.Table("user_info").Where("id = ?", id).First(&res).Error
	return
}
func (d Mysql) UpdatesUserByID(ID string, user *models.UserInfo) (err error) {
	err = d.Db.Table("user_info").Where("id = ?", ID).Updates(&user).Error
	return
}
func (d Mysql) SaveUserInfo(user *models.UserInfo) (err error) {
	err = d.Db.Table("user_info").Where("id = ?", user.ID).Save(&user).Error
	return
}
